$(function () {
    'use strict';

    $(document).on("pageInit", "#router-discover-mine", function (e, id, page) {

        // 获取ul应该设置的宽度
        var ul_with = 0;
        $("#scroll-departments ul li").each(function () {
            ul_with += $(this).outerWidth(true);
        })
        $("#scroll-departments ul").width(ul_with + 20);

        var myscroll = new IScroll('#scroll-departments', {
            scrollX: true,
            scrollY: false,
            mouseWheel: true,
            tap: true,
        });

        $(document).on("click", "#scroll-departments ul li", function () {
            $(this).addClass('active').siblings().removeClass('active');
            var index = $(this).index();
            initScrollJobTitle($(".scroll-job-titles").eq(index));
        });

        $("#scroll-departments ul li").on('tap', function () {
            $(this).addClass('active').siblings().removeClass('active');
            var index = $(this).index();
            initScrollJobTitle($(".scroll-job-titles").eq(index));
        });

        $(".scroll-job-titles ul li").on('tap', function () {
            var url = $(this).attr('data-url');
            window.location.href = url;
        });

        setTimeout(function () {
            $("#scroll-departments ul li.department-" + default_department).click();
        }, 100);

        function initScrollJobTitle(obj) {
            $(obj).show().siblings().hide();

            var ul_with = 0;
            $(obj).find("li").each(function () {
                ul_with += $(this).outerWidth(true);
            })

            $(obj).children('ul').width(ul_with + 20);

            var iscrollObj = $(obj).get(0);
            var scroll = new IScroll(iscrollObj, {scrollX: true, scrollY: false, mouseWheel: true, tap: true,});
        }

    })

    $(document).on("pageInit", "#router-discover", function (e, id, page) {
        // 设定左右两侧的div高度
        var leftHeight = $(window).height() - $('#router-discover .bar-nav').height() - $('#router-discover .buttons-tab').height() - $('#router-discover .bar-tab').height();
        $("#router-discover .scroll-departments").height(leftHeight);
        $("#router-discover .all-cats .right").height(leftHeight);

        // 选择分类的动画js
        $(document).on("click", ".discover .all-cats .left li", function () {
            var index = $(this).index();
            $(this).addClass('active').siblings().removeClass('active');
            $(this).parents('.left').next('.right').children('div').eq(index).show().siblings().hide();
        });

        // 选择分类，动态填充至input
        $(document).on("click", "#router-discover li.job", function () {
            $(this).toggleClass('active');

            var inputStr = '';
            $('#router-discover li.job.active').each(function () {
                var id = $(this).attr('data-id');
                inputStr += "<input name='category[]' type='hidden' value='" + id + "'>";
            });
            $('#category-inputs').html(inputStr);
        });
    });


    $(document).on("pageInit", "#router-user-avatar", function (e, id, page) {
        $("#router-user-avatar .avatar-list li").click(function () {
            $("#router-user-avatar .avatar-list li").removeClass('active');
            $(this).addClass('active');
            var img = $(this).find('img').attr('src');
            $("#form-avatar").find('input[name=avatar]').val(img);
        })
    })


    // 评论pop打开后自动获取焦点
    $(document).on('opened', '.popup-comment', function () {
        $(".popup-comment").find('textarea').addAttr('autofocus');
    });

    $.init();
});
